package com.yihuier.yihuier.Controller.UserManager;

import com.yihuier.yihuier.Controller.Common.FileController;
import com.yihuier.yihuier.Controller.Common.TimeJudgeController;
import com.yihuier.yihuier.Entity.UserManager.Manager;
import com.yihuier.yihuier.Service.RoomManager.CompanyService;
import com.yihuier.yihuier.Service.RoomManager.RoomService;
import com.yihuier.yihuier.Service.UserManager.ManagerService;
import com.yihuier.yihuier.Util.ResponseMessage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/UserManager/SuperManager/")
public class SuperManagerController {

    @Autowired
    CompanyService companyService;

    @Autowired
    ManagerService managerService;

    @Autowired
    RoomService roomService;

    @Autowired
    TimeJudgeController TimeStatusObj;

    @PostMapping("superManagerNameAndHeaderPic.json")
    public ResponseMessage getManagerNameAndHeaderPic(HttpServletRequest request){
        HttpSession session = request.getSession();
        String manager_id = (String)session.getAttribute("super_manager_id");
        if (manager_id == null){
            return new ResponseMessage(ResponseMessage.ERROR,"超级管理员账户登陆超时");
        }
        String manager_name = managerService.getManagerNameByManagerId(manager_id);
        if (manager_name == null){
            return new ResponseMessage(ResponseMessage.ERROR,"管理员账户已被注销");
        }
        LinkedHashMap<String,String> data = new LinkedHashMap<>();
        data.put("manager_name",manager_name);
        data.put("manager_header_pic","http://39.108.10.155:80/file/user_header_pic/superManager.jpg");
        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",data);
    }

    /**
     * 查看所有公司服务状态
     * */
    @PostMapping("reviewAllCompany.json")
    public ResponseMessage reviewAllCompany(HttpServletRequest request){
        HttpSession session = request.getSession();
        String super_manager_id = (String)session.getAttribute("super_manager_id");
        if (super_manager_id == null){
            return new ResponseMessage(ResponseMessage.ERROR,"超级管理员账户登陆超时");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设定时间格式规范
        List<Map<String,Object>> allCompany = companyService.getAllCompany();
        for (int i=0;i<allCompany.size();i++){
            allCompany.get(i).put("company_deadline",sdf.format(allCompany.get(i).get("company_deadline")));
        }
        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",allCompany);
    }

    /**
     * 添加服务公司
     * */
    @PostMapping("addServerCompany.do")
    public ResponseMessage addServerCompany(@PathParam("company_name") String company_name,@PathParam("company_deadline") String company_deadline,@PathParam("advertisement_company") int advertisement_company){
        int company_id = companyService.addCompanyReturnCompanyId(company_name,company_deadline,advertisement_company);
        if (company_id != 0){
            int addAIManagerResult = managerService.addAIManager(company_id);
            if (addAIManagerResult == 1){
                return new ResponseMessage(ResponseMessage.SUCCESS,"添加服务公司及智能管理员成功");
            }
            return new ResponseMessage(ResponseMessage.ERROR,"添加智能管理员失败");
        }
        return new ResponseMessage(ResponseMessage.ERROR,"添加服务公司失败");
    }

    /**
     * 查看所有管理员状态
     * */
    @PostMapping("reviewAllManagers.json")
    public ResponseMessage reviewAllManagers(HttpServletRequest request){
        HttpSession session = request.getSession();
        String super_manager_id = (String)session.getAttribute("super_manager_id");
        if (super_manager_id == null){
            return new ResponseMessage(ResponseMessage.ERROR,"超级管理员账户登陆超时");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设定时间格式规范
        List<Manager> Managers = managerService.selectManagersForSuperManager();
        ArrayList<Map<String,Object>> allManagers = new ArrayList<Map<String,Object>>();
        for (Manager littleManager:Managers){
            LinkedHashMap<String,Object> singleManager = new LinkedHashMap<String,Object>();
            singleManager.put("manager_id",littleManager.getManager_id());
            singleManager.put("manager_account",littleManager.getManager_account());
            singleManager.put("manager_username",littleManager.getManager_username());
            singleManager.put("manager_status",littleManager.getManager_status());
            singleManager.put("manager_type",littleManager.getManager_type());
            Map<String,String> companyInfo = companyService.getCompanyByCompanyId(littleManager.getManager_belongs_company());
            singleManager.put("manager_belongs_company",companyInfo.get("company_name"));
            singleManager.put("company_server_time",sdf.format(companyInfo.get("company_deadline")));
            try{
                Date dt1 = sdf.parse(sdf.format(companyInfo.get("company_deadline")));//公司服务时间
                Date dt2 = new Date();//现在时间
                if (dt1.getTime()>=dt2.getTime()){
                    singleManager.put("company_server_status",1);
                }else {
                    singleManager.put("company_server_status",0);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            singleManager.put("manager_company_property",companyInfo.get("advertisement_company"));
            allManagers.add(singleManager);
        }
        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",allManagers);
    }

    /**
     * 添加管理员
     * */
    @PostMapping("addManager.do")
    public ResponseMessage addManager(@PathParam("company_id") int company_id,@PathParam("manager_username") String manager_username,@PathParam("manager_account") String manager_account){
        int addResult = managerService.addManager(manager_username,company_id,manager_account);
        if (addResult == 1){
            return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功");
        }
        return new ResponseMessage(ResponseMessage.ERROR,"数据库错误");
    }

    /**
     * 添加管理员之前先显示管理员账号密码
     * */
    @PostMapping("beforeAddManager.json")
    public ResponseMessage beforeAddManager(@PathParam("company_id") int company_id){
        String newManagerAccount = managerService.beforeAddManager(company_id);
        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",newManagerAccount);
    }

    /**
     * 修改公司服务时间
     * */
    @PostMapping("setCompanyServerTime.do")
    public ResponseMessage setCompanyServerTime(@PathParam("company_id")String company_id,@PathParam("company_deadline")String company_deadline){
        int setResult = companyService.setCompanyServerTime(company_id,company_deadline);
        if (setResult == 1){
            if (TimeStatusObj.updateCompanyStatus(Integer.parseInt(company_id))){
                return new ResponseMessage(ResponseMessage.SUCCESS,"公司服务时间修改成功");
            }
            return new ResponseMessage(ResponseMessage.ERROR,"数据库错误");
        }
        return new ResponseMessage(ResponseMessage.ERROR,"数据库错误");
    }

    /**
     *在超级管理员页面显示所有的可用广告会议室
     */
    @PostMapping("reviewAllAdvertisementMeetingroom.json")
    public ResponseMessage reviewAllAdvertisementMeetingroom(HttpServletRequest request){
        HttpSession session = request.getSession();
        String super_manager_id = (String)session.getAttribute("super_manager_id");
        if (super_manager_id == null){
            return new ResponseMessage(ResponseMessage.ERROR,"超级管理员账户登陆超时");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设定时间格式规范
        List<Map<String,Object>> advertiseRooms = roomService.getAdvertisementRoom();
        for (int i=0;i<advertiseRooms.size();i++){
            advertiseRooms.get(i).put("room_pic_url", FileController.filePathDispose((String)advertiseRooms.get(i).get("room_pic_url")));//会议室照片url
            Map<String,String> companyInfo = companyService.getCompanyByCompanyId((Long)advertiseRooms.get(i).get("room_belong_company"));//会议室所属公司信息
            try{
                Date dt1 = sdf.parse(sdf.format(companyInfo.get("company_deadline")));//公司服务时间
                Date dt2 = new Date();//现在时间
                if (dt1.getTime()<dt2.getTime()){
                    String tempObj = ""+advertiseRooms.get(i).get("room_id");
                    int result = roomService.setRoomAdvertisementStatus(Integer.parseInt(tempObj),1);
                    advertiseRooms.remove(i);
                    continue;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            advertiseRooms.get(i).put("room_belong_company",companyInfo.get("company_name"));
        }
        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",advertiseRooms);
    }

    /**
     *在超级管理员页面重新选择可用广告会议室
     */
    @PostMapping("chooseAdvertisementRoom.do")
    public ResponseMessage chooseAdvertisementRooms(@PathParam("room_id") int room_id,@PathParam("room_advertisement") int room_advertisement,HttpServletRequest request){
        HttpSession session = request.getSession();
        String super_manager_id = (String)session.getAttribute("super_manager_id");
        if (super_manager_id == null){
            return new ResponseMessage(ResponseMessage.ERROR,"超级管理员账户登陆超时");
        }
        int AdvertisementCount = roomService.getAdvertisementRoomsCount();
        if (room_advertisement == 2){
            if (AdvertisementCount >= 4){
                return new ResponseMessage(ResponseMessage.ERROR,"最多只能选择四个会议室");
            }
            int updateResult = roomService.setRoomAdvertisementStatus(room_id,room_advertisement);
            if (updateResult != 0){
                return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",AdvertisementCount);
            }
        }
        int updateResult = roomService.setRoomAdvertisementStatus(room_id,room_advertisement);
        if (updateResult != 0){
            return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",AdvertisementCount);
        }
        return new ResponseMessage(ResponseMessage.ERROR,"数据库错误");
    }

    /**
     * 删除/取消删除管理员
     * */
    @PostMapping("setManagerStatus.do")
    public ResponseMessage setManagerStatus(@PathParam("manager_id") int manager_id,@PathParam("manager_status") int manager_status,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String super_manager_id = (String) session.getAttribute("super_manager_id");
        if (super_manager_id == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "超级管理员账户登陆超时");
        }
        int result = managerService.setManagerStatus(manager_id, manager_status);
        if (result == 1){
            return new ResponseMessage(ResponseMessage.SUCCESS, "请求成功");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "数据库错误");
    }

}
